﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Text;
using MyAspx.Framework.WebUnit.CodeZipTree;

namespace WebUI.Services.CodeTree
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class GetFileGrid : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {

            context.Response.ContentType = "text/plain";
            string dir = Explorer.GetCurrentPath(context);

            dir = Microsoft.JScript.GlobalObject.unescape(dir);
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(dir);
            StringBuilder sb = new StringBuilder(2000);
            sb.Append("<div class=\"jquery_filegrid\"><ul>\n");

            bool root = false;
            string backdir = Explorer.GetPrevDirectory(dir, ref root).ToNewPath();
            dir = dir.ToNewPath();
            if (!root)
                sb.AppendFormat("\t<li><div class=\"jquery_filegrid_back\"><div class=\"back\" rel=\"{0}\" orel=\"{1}\" relname=\"{1}\"></div></div></li>\n", backdir, dir);

            foreach (System.IO.DirectoryInfo di_child in di.GetDirectories())
                sb.AppendFormat("\t<li><div class=\"jquery_filegrid_border\"><div class=\"directory\" rel=\"{1}{0}/\"  relname=\"{2}\"></div></div><div class=\"jquery_filegrid_font\">{2}</div></li>\n", di_child.Name.ToLower(), dir, di_child.Name);
            foreach (System.IO.FileInfo fi in di.GetFiles())
            {
                string ext = "";
                if (fi.Extension.Length > 1)
                    ext = fi.Extension.Substring(1).ToLower();

                sb.AppendFormat("\t<li><div class=\"jquery_filegrid_border\"><div class=\"file ext_{0}\" rel=\"{2}{1}\"  relname=\"{3}\"></div></div><div class=\"jquery_filegrid_font\">{3}</div></li>\n", ext, fi.Name.ToLower(), dir, fi.Name);
            }
            sb.Append("</ul></div>");
            context.Response.Write(sb.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
